package com.lovo.service.impl;

import com.lovo.dao.UserDAO;
import com.lovo.dto.UserDTO;
import com.lovo.enums.UserCodeEnum;
import com.lovo.exception.UserException;
import com.lovo.model.UserDO;
import com.lovo.result.GlobalHandleException;
import com.lovo.result.ResultCode;
import com.lovo.service.UserService;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import javax.annotation.Resource;

/**
 * @title
 * @author: luo
 * @date: 2020/11/20 22:04
 */
@Service

public class UserServiceImpl implements UserService {
    @Resource
    private UserDAO userDAO;

    @Override
    @Transactional
    public UserDO login(UserDTO userDTO) throws GlobalHandleException {
        UserDO login = userDAO.login(userDTO);
        if (login== null) {
            throw new GlobalHandleException(ResultCode.USER_LOGIN_ERROR);
        }
        return login;
    }

    @Override
    @Transactional(rollbackFor = Exception.class)
    public void register(UserDTO userDTO) throws GlobalHandleException {
        Integer i = userDAO.register(userDTO);
        if ( i <= 0){
            throw new GlobalHandleException(ResultCode.USER_REGISTER_ERROR);
        }
    }



}
